package com.java.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.system.entity.User;
import com.java.system.entity.UserRole;
import com.java.system.mapper.UserMapper;
import com.java.system.mapper.UserRoleMapper;
import com.java.system.service.IUserService;
import com.java.system.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoman
 * @since 2023-07-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Override
    public List<User> select() {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        List<User> userList = userMapper.selectList(queryWrapper);
        if(!StringUtils.isEmpty(userList)){
            return userList;
        }
        return null;
    }

    @Override
    public void insertUser(User user) {
        String code = Md5Utils.code(user.getPassword());
        user.setPassword(code);
        userMapper.insert(user);
        List<Integer> roleIdList = user.getRoleIdList();
        if(roleIdList!=null){
            for(Integer roleId:roleIdList){
                userRoleMapper.insert(new UserRole(null,user.getId(),roleId));
            }
        }
    }

    @Override
    public User selectUserById(Integer id) {
        User user = userMapper.selectById(id);
        LambdaQueryWrapper<UserRole> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId,id);
        List<UserRole> userRoleList = userRoleMapper.selectList(queryWrapper);
        //拿出id
        List<Integer> list = userRoleList.stream().map(userRole -> {return userRole.getRoleId();
        }).collect(Collectors.toList());
        user.setRoleIdList(list);
        if(user!=null){
            return user;
        }
        return null;
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
        //清除原有角色
        LambdaQueryWrapper<UserRole> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId,user.getId());
        userRoleMapper.delete(queryWrapper);
        //设置新角色
        List<Integer> roleIdList = user.getRoleIdList();
        if(roleIdList!=null){
            for(Integer roleId:roleIdList){
                userRoleMapper.insert(new UserRole(null,user.getId(),roleId));
            }
        }
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
        LambdaQueryWrapper<UserRole> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId,id);
        userRoleMapper.delete(queryWrapper);
    }
}
